package com.dd.dao;

import java.sql.Timestamp;
import java.util.Hashtable;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dd.entity.DdUserorder;
import com.dd.util.PageList;

/**
 * A data access object (DAO) providing persistence and search support for
 * DdUserorder entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.dd.entity.DdUserorder
 * @author MyEclipse Persistence Tools
 */

public class DdUserorderDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(DdUserorderDAO.class);
	// property constants
	public static final String TYPE = "type";
	public static final String USERID = "userid";
	public static final String SHOPID = "shopid";
	public static final String GOODSLIST = "goodslist";
	public static final String ADDRESS = "address";
	public static final String NAME = "name";
	public static final String SEX = "sex";
	public static final String PHONE = "phone";
	public static final String PEOPLE = "people";
	public static final String LOGE = "loge";
	public static final String STATUS = "status";
	public static final String REMARK = "remark";
	public static final String ORDERID = "orderid";

	protected void initDao() {
		// do nothing
	}

	public void save(DdUserorder transientInstance) {
		log.debug("saving DdUserorder instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(DdUserorder persistentInstance) {
		log.debug("deleting DdUserorder instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public DdUserorder findById(java.lang.Integer id) {
		log.debug("getting DdUserorder instance with id: " + id);
		try {
			DdUserorder instance = (DdUserorder) getHibernateTemplate().get(
					"com.dd.entity.DdUserorder", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(DdUserorder instance) {
		log.debug("finding DdUserorder instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding DdUserorder instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from DdUserorder as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByType(Object type) {
		return findByProperty(TYPE, type);
	}

	public List findByUserid(Object userid) {
		return findByProperty(USERID, userid);
	}

	public List findByShopid(Object shopid) {
		return findByProperty(SHOPID, shopid);
	}

	public List findByGoodslist(Object goodslist) {
		return findByProperty(GOODSLIST, goodslist);
	}

	public List findByAddress(Object address) {
		return findByProperty(ADDRESS, address);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	public List findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	public List findByPeople(Object people) {
		return findByProperty(PEOPLE, people);
	}

	public List findByLoge(Object loge) {
		return findByProperty(LOGE, loge);
	}

	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	public List findByOrderid(Object orderid) {
		return findByProperty(ORDERID, orderid);
	}

	public List findAll() {
		log.debug("finding all DdUserorder instances");
		try {
			String queryString = "from DdUserorder";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public PageList findAll(String sqlwhere, int pageNo, int pageSize) {
		log.debug("finding all DdUserorder instances");
		try {
			PageList pl = new PageList();
			String queryString = "from DdUserorder a,DdShop b where a.shopid=b.id"
					+ sqlwhere;
			String maxsizeStr = (" select count(*) " + queryString);
			Query q = this.getSession().createQuery(maxsizeStr);
			int max = 0;
			List l = q.list();
			if (l != null && !l.isEmpty()) {
				max = (Integer) l.get(0);
			}
			q = this.getSession().createQuery(queryString);
			q.setFirstResult((pageNo - 1) * pageSize);
			q.setMaxResults(pageSize);
			List list = q.list();
			pl.addAll(list);
			pl.setTotalCount(max);
			return pl;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public DdUserorder merge(DdUserorder detachedInstance) {
		log.debug("merging DdUserorder instance");
		try {
			DdUserorder result = (DdUserorder) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(DdUserorder instance) {
		log.debug("attaching dirty DdUserorder instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(DdUserorder instance) {
		log.debug("attaching clean DdUserorder instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static DdUserorderDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (DdUserorderDAO) ctx.getBean("DdUserorderDAO");
	}
}